import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useDataStore = defineStore('data', () => {
  // 状态
  const coordinateData = ref([])
  const roomList = ref([])
  const areaLabelList = ref([])

  // 计算属性
  const filteredRoomList = computed(() => roomList.value)
  const roomCount = computed(() => roomList.value.length)

  // 动作
  const setCoordinateData = (data) => {
    coordinateData.value = data
  }

  const addCoordinatePoint = (point) => {
    coordinateData.value.push(point)
  }

  const setRoomList = (data) => {
    roomList.value = data
    // 自动更新区域标签列表
    areaLabelList.value = data.map((item, index) => ({
      name: item.name || `未命名区域 ${index + 1}`,
      index: index
    }))
  }

  const updateRoomList = (data) => {
    roomList.value = data
  }

  const setAreaLabelList = (data) => {
    areaLabelList.value = data
  }

  const filterRooms = (selectedIndexes) => {
    return roomList.value.filter((_, index) => selectedIndexes.includes(index))
  }

  const clearData = () => {
    coordinateData.value = []
    roomList.value = []
    areaLabelList.value = []
  }

  return {
    // 状态
    coordinateData,
    roomList,
    areaLabelList,
    // 计算属性
    filteredRoomList,
    roomCount,
    // 动作
    setCoordinateData,
    addCoordinatePoint,
    setRoomList,
    updateRoomList,
    setAreaLabelList,
    filterRooms,
    clearData
  }
})
